home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
WINPROGS
/
WUNZ20SR.ZIP
/
FILE_IO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-11
|
36KB
|
1,166 lines
/*---------------------------------------------------------------------------
file_io.c
This file contains routines for doing direct input/output, file-related
sorts of things. Most of the system-specific code for unzip is contained
here, including the non-echoing password code for decryption (bottom).
---------------------------------------------------------------------------*/
#if (!defined(__GO32__) && !defined(NeXT))
# define const
#endif
#define FILE_IO_C
#include "unzip.h"
#ifdef MSWIN
# include "wizunzip.h"
char __based(__segname("STRINGS_TEXT")) szDiskError[] = \
"A write error has occurred. Perhaps the disk is full. Continue ?";
#endif
/************************************/
/* File_IO Local Prototypes, etc. */
/************************************/
#if (!defined(DOS_OS2))
static int dos2unix __((unsigned char *buf, int len));
int CR_flag = 0; /* when last char of buffer == CR (for dos2unix()) */
#endif
#ifdef OS2
extern int longname; /* set in mapname.c */
extern char longfilename[];
#endif
#ifdef CRYPT
# if (defined(DOS_OS2) || defined(VMS))
# define MSVMS
# ifdef DOS_OS2
# ifdef __EMX__
# define getch() _read_kbd(0, 1, 0)
# else
# ifdef __GO32__
# include <pc.h>
# define getch() getkey()
# else /* !__GO32__ */
# include <conio.h>
# endif /* ?__GO32__ */
# endif
# else /* !DOS_OS2 */
# define getch() getc(stderr)
# define OFF 0 /* for echo control */
# define ON 1
# define echoff(f) echo(OFF)
# define echon() echo(ON)
# include <descrip.h>
# include <iodef.h>
# include <ttdef.h>
# if !defined(SS$_NORMAL)
# define SS$_NORMAL 1 /* only thing we need from <ssdef.h> */
# endif
# endif /* ?DOS_OS2 */
# else /* !(DOS_OS2 || VMS) */
# ifdef TERMIO /* Amdahl, Cray, all SysV? */
# ifdef CONVEX
# include <sys/termios.h>
# include <sgtty.h>
# else /* !CONVEX */
# ifdef LINUX
# include <termios.h>
# else /* !LINUX */
# include <sys/termio.h>
# endif /* ?LINUX */
# define sgttyb termio
# define sg_flags c_lflag
# endif /* ?CONVEX */
int ioctl OF((int, int, voidp *));
# define GTTY(f,s) ioctl(f,TCGETA,(voidp *)s)
# define STTY(f,s) ioctl(f,TCSETAW,(voidp *)s)
# else /* !TERMIO */
# if (!defined(MINIX) && !defined(__386BSD__))
# include <sys/ioctl.h>
# endif /* !MINIX && !__386BSD__ */
# include <sgtty.h>
# ifdef __386BSD__
# define GTTY(f, s) ioctl(f, TIOCGETP, (voidp *) s)
# define STTY(f, s) ioctl(f, TIOCSETP, (voidp *) s)
# else /* !__386BSD__ */
# define GTTY gtty
# define STTY stty
int gtty OF((int, struct sgttyb *));
int stty OF((int, struct sgttyb *));
# endif /* ?__386BSD__ */
# endif /* ?TERMIO */
int isatty OF((int));
char *ttyname OF((int));
# if (defined(PROTO) && !defined(__GNUC__) && !defined(_AIX))
int open (char *, int, ...);
# endif
int close OF((int));
int read OF((int, voidp *, int));
# endif /* ?(DOS_OS2 || VMS) */
#endif /* CRYPT */
/******************************/
/* Function open_input_file() */
/******************************/
int open_input_file() /* return non-zero if open failed */
{
/*
* open the zipfile for reading and in BINARY mode to prevent cr/lf
* translation, which would corrupt the bitstreams
*/
#ifdef VMS
zipfd = open(zipfn, O_RDONLY, 0, "ctx=stm");
#else /* !VMS */
#ifdef UNIX
zipfd = open(zipfn, O_RDONLY);
#else /* !UNIX */
#ifdef MACOS
zipfd = open(zipfn, 0);
#else /* !MACOS */
zipfd = open(zipfn, O_RDONLY | O_BINARY);
#endif /* ?MACOS */
#endif /* ?UNIX */
#endif /* ?VMS */
if (zipfd < 1) {
fprintf(stderr, "error: can't open zipfile [ %s ]\n", zipfn);
return (1);
}
return 0;
}
/**********************/
/* Function readbuf() */
/**********************/
int readbuf(buf, size)
char *buf;
register unsigned size;
{ /* return number of bytes read into buf */
register int count;
int n;
n = size;
while (size) {
if (incnt == 0) {
if ((incnt = read(zipfd, (char *)inbuf, INBUFSIZ)) <= 0)
return (n-size);
/* buffer ALWAYS starts on a block boundary: */
cur_zipfile_bufstart += INBUFSIZ;
inptr = inbuf;
}
count = MIN(size, (unsigned)incnt);
memcpy(buf, inptr, count);
buf += count;
inptr += count;
incnt -= count;
size -= count;
}
return (n);
}
#ifndef VMS /* for VMS use code in vms.c (old VMS code below is retained
* in case of problems...will be removed in a later release) */
/*********************************/
/* Function create_output_file() */
/*********************************/
int create_output_file() /* return non-0 if creat failed */
{
/*---------------------------------------------------------------------------
Create the output file with appropriate permissions. If we've gotten to
this point and the file still exists, we have permission to blow it away.
---------------------------------------------------------------------------*/
#if (!defined(DOS_OS2))
CR_flag = 0; /* hack to get CR at end of buffer working */
#endif
#if (defined(UNIX) && !defined(AMIGA))
{
int mask;
#ifndef VMS
if (!stat(filename, &statbuf) && (unlink(filename) < 0)) {
fprintf(stderr, "\n%s: cannot delete old copy\n", filename);
return 1;
}
# define EXTRA_ARGS
#else /* VMS */
# define EXTRA_ARGS ,"rfm=stmlf","rat=cr"
#endif /* ?VMS */
mask = umask(0); /* now know that we own it */
outfd = creat(filename, 0xffff & pInfo->unix_attr EXTRA_ARGS);
umask(mask); /* VMS, Unix */
}
#else /* !UNIX || AMIGA */ /* file permissions set after file closed */
#ifndef MACOS
outfd = creat(filename, S_IWRITE | S_IREAD); /* DOS, OS2, Mac, Amiga */
#else /* MACOS */
{
short fDataFork=TRUE;
MACINFO mi;
OSErr err;
fMacZipped = FALSE;
CtoPstr(filename);
if (extra_field &&
(lrec.extra_field_length > sizeof(MACINFOMIN)) &&
(lrec.extra_field_length <= sizeof(MACINFO))) {
BlockMove(extra_field, &mi, lrec.extra_field_length);
if ((makeword((byte *)&mi.header) == 1992) &&
(makeword((byte *)&mi.data) ==
lrec.extra_field_length-sizeof(ZIP_EXTRA_HEADER)) &&
(mi.signature == 'JLEE')) {
gostCreator = mi.finfo.fdCreator;
gostType = mi.finfo.fdType;
fDataFork = (mi.flags & 1) ? TRUE : FALSE;
fMacZipped = true;
/* If it was Zipped w/Mac version, the filename has either */
/* a 'd' or 'r' appended. Remove the d/r when unzipping */
filename[0]-=1;
}
}
if (!fMacZipped) {
if (!aflag)
gostType = gostCreator = '\?\?\?\?';
else {
#ifdef THINK_C
gostCreator = 'KAHL';
#else
#ifdef MCH_MACINTOSH
gostCreator = 'Manx';
#else
gostCreator = 'MPS ';
#endif
#endif
gostType = 'TEXT';
}
}
PtoCstr(filename);
outfd = creat(filename, 0);
if (fMacZipped) {
CtoPstr(filename);
if (hfsflag) {
HParamBlockRec hpbr;
hpbr.fileParam.ioNamePtr = (StringPtr)filename;
hpbr.fileParam.ioVRefNum = gnVRefNum;
hpbr.fileParam